<?php
/**
 * @auther:周卫星
 * @className:BizController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/10/28
 * @time:15:42
 */

namespace App\Http\Controllers\admin;


use App\Comment\Comment;
use App\Http\Controllers\Controller;
use App\Model\MallOrder;
use App\Model\SysBiz;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;

class BizController extends Controller
{
    public $biz_model;

    public function __construct()
    {
        $this -> biz_model = new SysBiz();
    }

    /**
     * 商户管理首页
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request -> isMethod('get')){
            return view('admin.biz.index');
        }
    }

    /**
     * 商户列表+分页+搜索
     * @param Request $request
     * @return BizController
     */
    public function lists(Request $request)
    {
        if ($request -> isMethod('get')){
            $biz_name = $request -> get('biz_name', '');
            $list = $this -> biz_model -> getBizList($biz_name);
            if ($list['total'] !== 0){
                $result = [
                    'code' => 0,
                    'message' => '获取成功',
                    'count' => $list['total'],
                    'data' => $list['data']
                ];
            }else{
                $result = [
                    'code' => 0,
                    'message' => '获取失败',
                    'count' => 0,
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function bizInfo(Request $request)
    {
        if ($request -> isMethod('get')){
            $id = $request -> route('id', '');
            $biz_info = $this -> biz_model -> getBizInfoById($id);
            // 获取商户当天的营业额
            $mall_order_model = new MallOrder();

            if ($request -> header('type') == 'ajax'){
                $start = $request -> get('start');
                $end = $request -> get('end');
                $turnover_today = $mall_order_model -> getTodayTurnover($id, $start, $end);
                if ($turnover_today){
                    $turnover_today_array = $turnover_today -> toArray();
                    // 获取微信的总营业额
                    $wx_turnover = array_sum(array_column($turnover_today_array, 'wxpay'));
                    // 获取支付宝的总营业额
                    $ali_turnover = array_sum(array_column($turnover_today_array, 'alipay'));
                    $card_turnover = array_sum(array_column($turnover_today_array, 'card'));
                    $cash_turnover = array_sum(array_column($turnover_today_array, 'cash'));
                    $charge_turnover = array_sum(array_column($turnover_today_array, 'charge'));
                }else{
                    $wx_turnover = 0;
                    $ali_turnover = 0;
                    $card_turnover = 0;
                    $cash_turnover = 0;
                    $charge_turnover = 0;
                }

                $total = floatval($wx_turnover) + floatval($ali_turnover) + floatval($cash_turnover) + floatval($card_turnover) + floatval($charge_turnover);
                $money_back = (floatval($wx_turnover) + floatval($ali_turnover)) - (sprintf("%.2f", $total * $biz_info['money_back'])) - (sprintf('%.2f', (floatval($wx_turnover) + floatval($ali_turnover)) * 0.006));
                return  $this -> returnJson(['code' => 0, 'msg' => '获取成功', 'data' => ['wx_turnover' => $wx_turnover, 'ali_turnover' => $ali_turnover, 'money_back' => $money_back, 'total' => $total]]);
            }else{
                $turnover_today = $mall_order_model -> getTodayTurnover($id);
                if ($turnover_today){
                    $turnover_today_array = $turnover_today -> toArray();
                    // 获取微信的总营业额
                    $wx_turnover = array_sum(array_column($turnover_today_array, 'wxpay'));
                    // 获取支付宝的总营业额
                    $ali_turnover = array_sum(array_column($turnover_today_array, 'alipay'));
                    $card_turnover = array_sum(array_column($turnover_today_array, 'card'));
                    $cash_turnover = array_sum(array_column($turnover_today_array, 'cash'));
                    $charge_turnover = array_sum(array_column($turnover_today_array, 'charge'));
                }else{
                    $wx_turnover = 0;
                    $ali_turnover = 0;
                    $card_turnover = 0;
                    $cash_turnover = 0;
                    $charge_turnover = 0;
                }

                $total = floatval($wx_turnover) + floatval($ali_turnover) + floatval($cash_turnover) + floatval($card_turnover) + floatval($charge_turnover);
                $money_back = (floatval($wx_turnover) + floatval($ali_turnover)) - (sprintf("%.2f", $total * $biz_info['money_back'])) - (sprintf('%.2f', (floatval($wx_turnover) + floatval($ali_turnover)) * 0.006));

                return view('admin.biz.detail', ['info'=> $biz_info, 'wx_turnover' => $wx_turnover, 'ali_turnover' => $ali_turnover, 'money_back' => $money_back, 'total' => $total]);
            }
        }
    }

    /**
     * 修改商户的信息
     * @param Request $request
     * @return BizController
     */
    public function updateBiz(Request $request)
    {
        if ($request -> isMethod('post')){
            $id = $request -> post('id');
            $set_data = $request -> post('set');
            $bol = $this -> biz_model -> updateBizById($id, $set_data);
            if ($bol){
                $result = [
                    'code' => 0,
                    'message' => '修改成功',
                    'count' => 0,
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 0,
                    'message' => '修改失败',
                    'count' => 0,
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    public function addBiz(Request $request)
    {
        if ($request -> isMethod('get')){
            return view('admin.biz.add');
        }
        if ($request -> isMethod('post')){
            $data = $request -> post();
            if ($data['password'] !== $data['confirm_pwd']){
                $result = [
                    'code' => 1,
                    'msg' => '密码与确认密码不一致,请重新输入',
                    'data' => []
                ];
                return $this -> returnJson($result);
            }
            $date_time = time();

            // 组装sys_biz的数据
            $biz_data = [
                'name' => $data['name'],
                'category' => $data['category'],
                'truename' => $data['truename'],
                'tel' => $data['tel'],
                'address' => $data['address'],
                'longitude' => $data['longitude'],
                'latitude' => $data['latitude'],
                'idcard' => $data['idcard'],
                'idfront' => $data['idfront'],
                'idback' => $data['idback'],
                'business_license_name' => $data['business_license_name'],
                'business_license_code' => $data['business_license_code'],
                'business_license_address' => $data['business_license_address'],
                'business_license_expire' => strtotime($data['business_license_expire']),
                'business_license_image' => $data['business_license_image'],
                'catering_service_license_name' => $data['catering_service_license_name'],
                'catering_service_license_no' => $data['catering_service_license_no'],
                'catering_service_license_address' => $data['catering_service_license_address'],
                'catering_service_license_expire' => strtotime($data['catering_service_license_expire']),
                'catering_service_license_image' => $data['catering_service_license_image'],
                'create_time' => $date_time,
                'update_time' => $date_time
            ];

            // 组装biz_member的数据
            $biz_member = [
                'username' => $data['username'],
                'name' => $data['name'],
                'password' => Comment::member_md5($data['password'], config('params.password_key')),
                'is_staff' => 0,
                'create_time' => $date_time,
                'update_time' => $date_time
            ];
            if ($this -> biz_model -> addBiz($biz_data, $biz_member)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    public function updateBizInfo(Request $request)
    {
        if ($request -> isMethod("get")){
            $id = $request -> route("id");
            $info = $this -> biz_model -> getBizInfoById($id);
            return view('admin.biz.update', ['info' => $info, 'id' => $id]);
        }
        if ($request -> isMethod("post")){
            $data = $request -> post();
            $id = $data['id'];
            $date_time = time();

            // 组装sys_biz的数据
            $biz_data = [
                'name' => $data['name'],
                'category' => $data['category'],
                'truename' => $data['truename'],
                'tel' => $data['tel'],
                'address' => $data['address'],
                'longitude' => $data['longitude'],
                'latitude' => $data['latitude'],
                'idcard' => $data['idcard'],
                'idfront' => $data['idfront'],
                'idback' => $data['idback'],
                'business_license_name' => $data['business_license_name'],
                'business_license_code' => $data['business_license_code'],
                'business_license_address' => $data['business_license_address'],
                'business_license_expire' => strtotime($data['business_license_expire']),
                'business_license_image' => $data['business_license_image'],
                'catering_service_license_name' => $data['catering_service_license_name'],
                'catering_service_license_no' => $data['catering_service_license_no'],
                'catering_service_license_address' => $data['catering_service_license_address'],
                'catering_service_license_expire' => strtotime($data['catering_service_license_expire']),
                'catering_service_license_image' => $data['catering_service_license_image'],
                'update_time' => $date_time
            ];

            // 组装biz_member的数据
            $biz_member = [
                'username' => $data['username'],
                'name' => $data['name'],
                'update_time' => $date_time
            ];
            if ($this -> biz_model -> updateBizId($id, $biz_data, $biz_member)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }
}
